package artin.A3D7.main
{



/**
 * Engine:Alternativa3D7.5.1
 * Author:Mars Yang
 * Link:http://www.marsyang.com/blog/archives/214
 */

	import alternativa.engine3d.containers.BSPContainer;
	import alternativa.engine3d.controllers.SimpleObjectController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Object3DContainer;
	import alternativa.engine3d.core.View;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.loaders.MaterialLoader;
	import alternativa.engine3d.loaders.ParserCollada;
	import alternativa.engine3d.objects.Mesh;
	import artin.A3D7.serices.DragController;
	//import com.greensock.*;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	[SWF(backgroundColor="#000000", frameRate="100", width="550", height="400")]
	
	public class LoaderTank3D extends Sprite
	{
		private static const PATH_URL:String = "resources/model/tank/";
		private static const COLLADA_URL:String = "tank.dae";
		
		private var rootContainer:Object3DContainer = new Object3DContainer();
		private var camera:Camera3D;
		private var scene:BSPContainer;
		private var controller:SimpleObjectController;
		private var userCamera:Object3D = new Object3D();
		private var dragger:DragController;
		private var parser:ParserCollada;
		private var loader:URLLoader;
		private var materialLoader:MaterialLoader;
		
		public function LoaderTank3D()
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		public function init(e:Event): void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			camera = new Camera3D();
			camera.view = new View(stage.stageWidth, stage.stageHeight);
			addChild(camera.view);
			addChild(camera.diagram);
			
			controller = new SimpleObjectController(stage, camera, 1);
			
			scene = new BSPContainer();
			scene.addChild(camera);
			
			dragger = new DragController(stage, 0, 0, 100);
			dragger.enabled = true;
			
			var v:Vector3D = dragger.position;
			userCamera.x = v.x;
			userCamera.y = v.y;
			userCamera.z = v.z;
			
			loadCollada();
			camera.render();
		}
		private function loadCollada() : void
		{
			loader = new URLLoader();
			loader.load(new URLRequest(PATH_URL+COLLADA_URL));
			loader.addEventListener(Event.COMPLETE, onLoadCollada);
		}
		private function onLoadCollada(e:Event) : void
		{
			//解析模型文件
			parser = new ParserCollada();
			parser.parse(new XML(loader.data), PATH_URL);//注意这个地方，PATH_URL是你要加载的贴图路径
			//导入贴图，如果模型不自带贴图的话，需要另外加载贴图文件
			materialLoader = new MaterialLoader();
			materialLoader.addEventListener(Event.COMPLETE, onCompleteMaterial);
			materialLoader.load(parser.textureMaterials, new LoaderContext(true));
		}
		private function onCompleteMaterial(e:Event) : void
		{
			//遍历所有模型里的对象
			for each(var o:Object3D in parser.objects) {
				if (!o.name) continue;
				scene.addChild(o);
			}
			addEventListener(Event.ENTER_FRAME, onTick);
		}
		private function onTick(e:Event):void 
			
		{
			var v:Vector3D = dragger.position;
			userCamera.x += (v.x - userCamera.x) * 0.3;
			userCamera.y += (v.z - userCamera.y) * 0.3;
			userCamera.z += (v.y - userCamera.z) * 0.3;
			controller.setObjectPosXYZ(userCamera.x, userCamera.y, userCamera.z);
			controller.lookAtXYZ(0, 0, 0);
			camera.render();
		}
	}
}
